<template>
  <div class="pageFullScreen" id="xxxFullScreen">
    <el-button type="primary" class="add-role" @click="createRole"
      >创建角色</el-button
    >
    <el-dialog title="提示" :visible.sync="createRoleDig" width="60%">
      <p>请输入要创建的角色名称:</p>
      <el-input v-model="newroleName"></el-input>
      <p>设置角色权限列表:</p>
      <el-tree
        :data="asideBar"
        show-checkbox
        node-key="id"
        ref="tree"
        default-expand-allid
        :expand-on-cliddck-node="false"
        :default-checked-keys="defaultChoose"
        :default-expanded-keys="defaultChoose"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="createRoleDig = false">取 消</el-button>
        <el-button type="primary" @click="submitNewRole">确定创建</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {customAsideData}  from "../../plugins/roleManage"
export default {
  props: ["defaultTemplate"],
  data() {
    return {
      newroleName: "",
      createRoleDig: false,
      asideBar: [],
      defaultChoose: [],
      createRoleAside: {},
    };
  },
  activated() {},
  methods: {
    submitNewRole() {
      let tmpData = this.$refs.tree.getCheckedNodes(false, false);
      let leafNode = this.$refs.tree.getHalfCheckedNodes();
     let dataList= customAsideData(tmpData, leafNode)
      if(this.newroleName == null || this.newroleName == "")
      {
        this.$message.warning("请输入角色名")

      }else
      {
        this.$axios({
          url: "/role/add_role.action",
          method: "post",
          data: {
            roleName: this.newroleName,
            roleList: JSON.stringify(dataList),
          },
        }).then((res) => {
          if (res.data.status) {
            this.$emit('getAllRoles');
            this.createRoleDig = false;
            this.newroleName = ""
            this.$message({
              message: "添加成功",
              type: "success",
            });
          } else {
            this.$message({
              message: "添加失败",
              type: "error",
            });
          }
        });
      }

    },
    createRole() {
      this.asideBar = [];
      this.createRoleDig = true;
      let defaultTemplateData = eval("(" + this.defaultTemplate + ")");
      let asideBarData = defaultTemplateData.AsideBar;
      Object.keys(asideBarData).forEach((key, index) => {
        let childrenObj = {};
        let itemObj = {};
        itemObj["id"] = index;
        itemObj["label"] = key;
        itemObj["children"] = [];
        let itemValue = asideBarData[key];
        for (let ckey in itemValue) {
          let childrenItem = {};
          childrenItem["id"] = ckey;
          childrenItem["label"] = itemValue[ckey];
          itemObj["children"].push(childrenItem);
        }
        this.defaultChoose.push(index);
        this.asideBar.push(itemObj);
      });
    },
  },

}
</script>
<style>
</style>
